package server.phone.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.utopia84.model.Saleandpdt;
import com.utopia84.model.Salerecord;
import com.utopia84.utils.MyDate;

/**
 * @author utopia
 * 
 */
public class SaleAndPdtDAO {
	DBHelper manager;
	String sql = "";
	ResultSet rs;

	public SaleAndPdtDAO() throws IOException, ClassNotFoundException,
			SQLException {
		manager = DBHelper.createInstance();
	}

	/**
	 * 返回所有销售记录和菜单信息
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Saleandpdt> getAllSaleAndPdt(String createTime)
			throws SQLException {
		List<Saleandpdt> sales = new ArrayList<Saleandpdt>();
		Saleandpdt sale = new Saleandpdt();
		sql = "select * from Saleandpdt where createTime > ? order by createTime limit 0,100";
		Object[] params = new Object[] {createTime};
		manager.connectDB();
		rs = manager.executeQuery(sql,params);
		while (rs.next()) {
			sale = new Saleandpdt(
					rs.getInt("contactNumber"),
					rs.getInt("id"),rs.getString("salerecordId"),rs.getString("pdtCode"),
					rs.getString("pdtName"),rs.getInt("number"),rs.getFloat("price"),
					rs.getString("status"),rs.getInt("priority"),rs.getString("otherspec"),
					rs.getString("otherspec1"),rs.getString("otherspec2"),
					rs.getString("createTime"),rs.getString("closeTime")
					);
			sales.add(sale);
		}
		manager.closeDB();
		return sales;
	}

	/**
	 * 插入销售记录和菜单信息
	 * 
	 * @return
	 * @throws SQLException
	 */

	public int saveSaleAndPdt(List<Saleandpdt> records) throws SQLException {
		int result = 0;
		final int size = records.size();
		sql = "insert into Saleandpdt (`id`,`salerecordId`,`pdtCode`,`pdtName`,`number`,`price`,`status`,"
				+"`priority`,`createTime`,`closeTime`,`otherspec`,`otherspec1`,`otherspec2`,`contactNumber`)"
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		Object[][] params = new Object[size][14];
		for (int i = 0; i < records.size(); i++) {
			params[i][0] = records.get(i).getId();//int
			params[i][1] = records.get(i).getSalerecordId();
			params[i][2] = records.get(i).getPdtCode();
			params[i][3] = records.get(i).getPdtName();
			params[i][4] = records.get(i).getNumber();//int
			params[i][5] = records.get(i).getPrice();//float
			params[i][6] = records.get(i).getStatus();
			params[i][7] = records.get(i).getPriority();//int
			params[i][8] = records.get(i).getCreateTime();
			params[i][9] = records.get(i).getCloseTime();
			params[i][10] = records.get(i).getOtherspec();
			params[i][11] = records.get(i).getOtherspec1();
			params[i][12] = records.get(i).getOtherspec2();
			params[i][13] = records.get(i).getContactNumber();
		}
		manager.connectDB();
		result = manager.insertSaleAndPdt(sql, params);
		manager.closeDB();
		return result;
	}

	public boolean updateSaleAndPdt(String salerecordId, String pdtCODE,
			String closeTime, String createTime, String status) {
		boolean result = false;
		String sql = "update  Saleandpdt set status=?,closeTime=? where salerecordId=? and pdtCode=? and createTime=? ";
		try {
			manager.connectDB();
			result = manager.executeUpdate(sql, new Object[] { status,closeTime,salerecordId,pdtCODE,createTime});
			manager.closeDB();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
	
	public boolean isDrink(String ptdCode)
			throws SQLException {
		String result = "";
		sql = "Select TypeName from Product as p join Menutype as mt on mt.TypeId=p.typeId where p.pdtCode=?";
		Object[] params = new Object[] { ptdCode };
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next()) {
			result = rs.getString(1);
		}
		manager.closeDB();
		return result.equals("Drink");
	}
}